package cn.aihongbo.kmp.data.vm

import androidx.compose.runtime.mutableStateListOf
import cn.aihongbo.kmp.base.vm.HViewModel
import cn.aihongbo.kmp.data.bean.CitiesBaseDiy
import cn.aihongbo.kmp.data.bean.CityBean
import cn.aihongbo.kmp.data.repository.CityRepository

class CityViewModel(private val repository: CityRepository) : HViewModel() {

    val citiesBaseDiy = mutableStateListOf<CitiesBaseDiy?>()
    val hotCities = mutableStateListOf<CityBean?>()
    val searchCities = mutableStateListOf<CityBean?>()

    fun loadCitiesByCountry() {
        net {
            repository.getCitiesByCountry().collect {
                it?.list?.let { elements -> hotCities.addAll(elements) }
            }
        }
    }

    fun loadBaseDataDiy() {
        net {
            repository.getBaseDataDiy().collect {
                citiesBaseDiy.addAll(it)
            }
        }
    }

    fun searchCities(keyword: String) {
        if (keyword.isNotEmpty()) {
            net {
                repository.getCitiesSearch(keyword).collect {
                    searchCities.clear()
                    searchCities.addAll(it)
                }
            }
        } else {
            searchCities.clear()
        }
    }
}